cmake_minimum_required (VERSION 2.8)
set (pname ClaplanEditor)

project (${pname} CXX)

set (PROG_VERSION_MAJOR 0)
set (PROG_VERSION_MINOR 1)
set (PROG_VERSION_PATCH 1)
set (PROG_VERSION "${PROG_VERSION_MAJOR}.${PROG_VERSION_MINOR}.${PROG_VERSION_PATCH}")

message (STATUS "Buiding program ${pname} version ${LIB_VERSION}")

# Look for the wxWidget package
find_package(wxWidgets COMPONENTS core base REQUIRED)

if (wxWidgets_FOUND)
    include(${wxWidgets_USE_FILE})
    message(STATUS "wxWidget library : ${wxWidgets_LIBRARIES}")
    message(STATUS "wxWidget include dir : ${wxWidgets_USE_FILE}")
    message(STATUS "wx-config path : ${wxWidgets_CONFIG_EXECUTABLE}")
else(wxWidgets_FOUND)
    message(FATAL_ERROR "wxWidgets not found")    
endif(wxWidgets_FOUND)


# Compiler flags
option(GPROF "Profile the application using GProf" OFF)
set (cxxflags "-Wall -g -O2 -D_LARGE_FILES -D__WXGTK__")
if (${GPROF})
    message(STATUS "Profiling will be done using : GProf")
    set (cxxflags "${cxxflags} -pg")
    set (CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -pg")
endif(${GPROF})
set (CMAKE_CXX_FLAGS ${cxxflags})
set (CMAKE_CXX_FLAGS_DEBUG ${cxxflags})
set (CMAKE_CXX_FLAGS_RELEASE ${cxxflags})

# Look for stringmanip library and include files
find_library(STRINGMANIP_LIBRARY  
    NAMES stringmanip
    PATHS $ENV{HOME}/git/stringmanip/Release
    NO_DEFAULT_PATH
)
set (STRINGMANIP_INCLUDE_DIR $ENV{HOME}/git/stringmanip )

message(STATUS "stringmanip library : ${STRINGMANIP_LIBRARY}")
message(STATUS "stringmanip include dir : ${STRINGMANIP_INCLUDE_DIR}")

include_directories("${PROJECT_SOURCE_DIR}" ${STRINGMANIP_INCLUDE_DIR} ${wxWidgets_INCLUDE_DIRS})

set (SRCS claplan_header.cc claplan_parser.cc claplan_data.cc 
    claplan_target.cc claplan_oar.cc claplan_beam.cc claplan_print.cc
    claplan_optimization.cc)
set (GUI_SRCS claplan_view.cc claplan_tree.cc claplan_controller.cc ${SRCS})
set (LIBS ${STRINGMANIP_LIBRARY})
set (GUI_LIBS ${wxWidgets_LIBRARIES})

# add the executable
add_executable (test_claplan_parser test_claplan_parser.cc ${SRCS})
target_link_libraries (test_claplan_parser ${LIBS})

# GUI executable
add_executable (test_claplan_view test_claplan_view.cc ${GUI_SRCS})
target_link_libraries (test_claplan_view ${GUI_LIBS} ${LIBS})


# copy a claplan.cl file to the build directory
file (COPY "${CMAKE_CURRENT_SOURCE_DIR}/claplan.cl" DESTINATION "${CMAKE_BINARY_DIR}/")


# Generate documentation
option(BUILD_DOCUMENTATION 
    "Create and install the HTML based API documentation (requires Doxygen)" OFF)
IF(BUILD_DOCUMENTATION)
 
  FIND_PACKAGE(Doxygen)
  IF(NOT DOXYGEN_FOUND)
    MESSAGE(FATAL_ERROR
      "Doxygen is needed to build the documentation.")
  ENDIF()
  
 
  SET( doxyfile             ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile )
  SET( doxy_html_index_file ${CMAKE_CURRENT_SOURCE_DIR}/Doc/html/index.html )
  
  message(STATUS "Building documentation : ${doxy_html_index_file}")
 
  ADD_CUSTOM_COMMAND( OUTPUT ${doxy_html_index_file}
                      COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
                      MAIN_DEPENDENCY ${doxyfile}
                      DEPENDS test_claplan_parser
                      COMMENT "Generating HTML documentation")
 
  ADD_CUSTOM_TARGET( doc ALL DEPENDS ${doxy_html_index_file} )
 
  #INSTALL( DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc DESTINATION share/doc )
ENDIF()

# Tests
macro (do_test testname programname result)
  add_test (${testname} ${programname} ${arg})
  set_tests_properties (${testname}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)

do_test (testClaplanParser test_claplan_parser "Parser test passed")

